import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import router from './router';
import App from './App.vue'
import { createPinia } from 'pinia'
//import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import { createPersistedState } from 'pinia-plugin-persistedstate'
// 引入 Mock 数据
import './mock'
import { ReloadData,useAllDataStore } from '@/stores';
import { i18n } from '@/locales';

// 创建 Pinia 实例
const pinia = createPinia();

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }



  //  app.use(router);
  // 使用插件
  // pinia.use(piniaPluginPersistedstate);
  pinia.use(createPersistedState({
   key: id => `__persisted__${id}`,
    storage: sessionStorage,
  }))
  app.use(pinia);
// 国际化
 // 设置 i18n 的 locale
let currentLanguage = navigator.language.replace(/-(\S*)/, '');
// 如果本地缓存记录了语言环境，则使用本地缓存
const Store = useAllDataStore();
let lsLocale = Store.getLocale() || '';
console.log('lsLocale', lsLocale);
if (lsLocale) {
  try {
      const parsedLocale = JSON.parse(lsLocale);
      currentLanguage = parsedLocale?.curLocale || 'zh'; // 确保有默认值
  } catch (error) {
      // console.error('Failed to parse lsLocale:', error);
      currentLanguage = lsLocale; // 如果解析失败，直接使用 lsLocale
  }
}

// 校验语言是否合法，仅允许 "zh" 或 "en"
const allowedLocales: ("zh" | "en")[] = ["zh", "en"];
currentLanguage = allowedLocales.includes(currentLanguage as "zh" | "en")
    ? currentLanguage
    : "zh"; // 如果非法，默认设置为 "zh"

// 设置 i18n 的 locale
i18n.global.locale.value = currentLanguage as "zh" | "en";

  ReloadData();
  app.use(router);
  app.use(ElementPlus, {
    locale: zhCn,
  })
  app.use(i18n);
  

app.mount('#app')